/*
 * @Author: AskMeWhy
 * @Date: 2020-12-27 11:58:47
 * @Last Modified by: AskMeWhy
 * @Last Modified time: 2021-01-08 14:14:43
 */
const app = getApp();
const { AskComponent } = app._require("common/index");
AskComponent({
    properties: {
        placeholder: {
            type: String,
            value: "",
        },
        title: {
            type: String,
            value: "标题",
        },
        list: {
            type: Array,
            value: [],
        },
        value: {
            type: Array,
            value: [],
            observer: "formatValue",
        },
        signle:{
            type: Boolean,
            value:true
        },
    },
    data: {
        valueText: "",
        scrollTop: 0,
        show: false,
        chose_imgsr:"/assets/icon/s_red.png"
    },
    methods: {
        formatValue() {
            let valueText = "";
            if (this.data.value.length > 0) {

                let res = this.data.list.filter(cur => this.data.value.includes(cur.value));
                // console.log(res);

                if (res && res.length > 0) {
                    res.forEach(element => {
                        if(valueText.length == 0){valueText = element.value}
                        else{valueText = valueText+","+element.value}
                        // console.log(element.value);
                    });
                    // valueText = "已选中(" + res.length + '/' + this.data.list.length + ")个";
                }
            }

            this.setData({
                valueText,
            });
        },
        handleClick(e) {
            let { item } = e.currentTarget.dataset;
            this.$emit("click", { value: item });
        },
        showPopup() {
            let scrollTop = this.data.scrollTop;
            let _index = -1;
            if (this.data.value) {
                _index = this.data.list.findIndex(cur => this.data.value.includes(cur.value));
                // if (_index !== -1) {
                //     scrollTop = _index * 44;
                // }
            }
            this.setData({
                show: true,
            });
            setTimeout(() => {
                if(_index !== -1){
                    this.getRect(".ask-select__item-box", true).then(r => {
                        this.setData({
                            scrollTop: r[_index].top - r[0].top,
                        });
                    }).catch(r=>{
                        this.setData({
                            scrollTop: scrollTop,
                        });
                    });
                }
            }, 100);
        },
        onClose() {
            this.setData({ show: false });
        },
        moveHandle () {
            // console.log("111")
        },
        clickSureAction () {
            console.log(this.data.value);
            let value = this.data.value;
            if(value.length==0){
                wx.showToast({
                  title: '选项不能为空',
                  icon:"none"
                })
            }else{
                this.onClose();
            }
            //
        },
        clickclearAction (e) {
            console.log(e);
            // console.log("清空");
            // let { item } = e.currentTarget.dataset;
            this.$emit("click", {isClear:"1"});
        }
    },
});
